package cn.sinjet.carassist;

import java.util.ArrayList;
import java.util.List;

import com.mgroup.wazehelper.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import cn.sinjet.adapter.MyObdListAdapter;
import cn.sinjet.database.DAOFactory;
import cn.sinjet.database.OBDDAO;
import cn.sinjet.model.carsystem.CarModel;
import cn.sinjet.prototol.codec.ToureDevCodec;
import cn.sinjet.viewmodel.PageDefine;
import cn.sinjet.viewmodel.ViewModel;

public class ErrDectectSystemActivity extends MyActivity implements OnClickListener{
	SinjetApplication app;
//	Button btnBack;
	ListView listViewOBD;
	List<String> listOBDData = new ArrayList<String>();
	MyObdListAdapter listAdapterOBD;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setMyContentView(R.layout.activity_err_detect_system);
		app = SinjetApplication.getInstance();
		
		listViewOBD = (ListView)mViewRoot.findViewById(R.id.obd_search_listview_obd);
		listAdapterOBD = new MyObdListAdapter(this,listOBDData);
		listViewOBD.setAdapter(listAdapterOBD);
		new Handler().postDelayed(loadOBDDataRunnable, 50);
	}
	
	Runnable loadOBDDataRunnable = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			loadOBDData();
		}
	};
	
	
	private void  loadOBDData(){
		OBDDAO obdDao = DAOFactory.getInstance().getOBDDao();
		if(obdDao == null)
			return;
        Log.d("ing", "loadOBDData detectedErrOBD");
		listAdapterOBD.setListData(CarModel.getInstance().devModule.getObdData().getDectectedErrObdList());
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ViewModel.getIns().onActivityResume(this, PageDefine.PAGE_CAR_ERR_DETECT, this);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
	
	@Override
	public void updateUI(Bundle data) {
		// TODO Auto-generated method stub
		super.updateUI(data);
		if(data.getInt("id",-1)==R.id.obd_search_listview_obd){
		
			List<String> obdListData = new ArrayList<String>();
			obdListData.addAll(CarModel.getInstance().devModule.getObdData().getDectectedErrObdList());
			Log.d("ing", "refresh detected err data size:"+obdListData.size());
			listAdapterOBD.setListData(obdListData);
			listAdapterOBD.notifyDataSetChanged();
		}
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(view.getId() == R.id.err_detected_clear_err){
			new AlertDialog.Builder(this).setTitle(null)
			.setMessage(getResources().getString(R.string.clear_fault_warning))
			.setPositiveButton(getResources().getString(R.string.yes),
			new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,int which) {
					new ToureDevCodec().sendClearErr();
				}
			}
			).setNegativeButton(getResources().getString(R.string.no), null).show();
		}
	}
	

}
